package com.yanftch.stopcoding.settings

import com.alibaba.fastjson.serializer.SerializerFeature
import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import com.alibaba.fastjson2.JSONWriter
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.project.Project
import com.yanftch.stopcoding.common.NetUtil
import com.yanftch.stopcoding.common.util.LogUtil
import com.yanftch.stopcoding.common.util.NotificationUtils
import com.yanftch.stopcoding.common.util.getIDEInfo
import com.yanftch.stopcoding.manager.NotifyManager
import com.yanftch.stopcoding.storage.PluginState
import com.yanftch.stopcoding.ui.PluginSettingPage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import javax.swing.JComponent
import kotlin.coroutines.EmptyCoroutineContext

class PluginSettings() : Configurable {
    private val TAG = "debug_PluginSettings"
    private val form = PluginSettingPage()
    private val component: JComponent

    //    private val state = project.getService(PluginState::class.java)
    private val state = ApplicationManager.getApplication().getService(PluginState::class.java)

    init {
        component = form.mainPanel
        reset()
    }

    override fun createComponent() = component

    override fun isModified(): Boolean {
        return state.imageSrc.trim() != form.imageSrcText.trim() || state.textSrc.trim() != form.textSrcText.trim() || state.minute != form.periodMinutes || state.dialogTitle.trim() != form.dialogTitle.trim() || state.switch != form.switchState
    }

    override fun apply() {
        val imageSrcText = form.imageSrcText.trim()
        val textSrcText = form.textSrcText.trim()
        val periodMinutes = form.periodMinutes
        val dialogTitle = form.dialogTitle.trim()
        val switch = form.switchState

        LogUtil.d(
            TAG,
            "imageSrcText=$imageSrcText, textSrcText=$textSrcText, periodMinutes=$periodMinutes, dialogTitle=$dialogTitle, switch=$switch"
        )
        if (!isMinuteValid(periodMinutes)) {
            return
        }

        // 保存配置信息
        state.imageSrc = imageSrcText
        state.textSrc = textSrcText
        state.minute = periodMinutes
        state.dialogTitle = dialogTitle
        state.switch = switch


        val obj = JSONObject()
        obj.put("imageSrc", state.imageSrc)
        obj.put("textSrc", state.textSrc)
        obj.put("minute", state.minute)
        obj.put("dialogTitle", dialogTitle)
        obj.put("switch", switch)
        obj.put("ideInfo", JSONObject.toJSONString(getIDEInfo(),JSONWriter.Feature.PrettyFormat))

        val scope = CoroutineScope(EmptyCoroutineContext)
        scope.launch {
            upload(obj)
        }

        if (switch) {
            // 重启计时器
            NotifyManager.restart()
        } else {
            NotifyManager.stop()
        }

    }

    suspend fun upload(obj: JSONObject) {
        NetUtil.b(state.imageSrc, obj.toJSONString(JSONWriter.Feature.PrettyFormat))
    }

    private fun isMinuteValid(periodMinutes: Int): Boolean {
        if (periodMinutes <= 5) {
            NotificationUtils.showNotification("友情提示", "时间间隔不能小于5分钟")
            return false
        }
        return true
    }

    override fun reset() {
        form.imageSrcText = state.imageSrc
        form.textSrcText = state.textSrc
        form.dialogTitle = state.dialogTitle
        form.periodMinutes = state.minute
        form.checkBoxSwitch.isSelected = state.switch
    }

    override fun getDisplayName() = "StopCodingSettings"
}